import {
  getSystemMonitorData, 
  getSystemResources, 
  getServiceStatuses, 
  restartService as apiRestartService, 
  getSystemLogs, 
  getNetworkStats, 
  getStorageVolumes, 
  getDatabaseStats, 
  addSystemLogEntry 
} from './api';

// 系统资源数据类型
export interface SystemResourceData {
  cpu: number;
  memory: number;
  disk: number;
  status: 'normal' | 'warning' | 'error';
  uptime: string;
  lastUpdated: string;
}

// 服务状态数据类型
export interface ServiceStatus {
  name: string;
  status: 'running' | 'stopped' | 'warning';
  uptime: string;
  memory: number;
  cpu: number;
  restarts: number;
}

// 日志条目数据类型
export interface LogEntry {
  id: string;
  timestamp: string;
  level: 'info' | 'warning' | 'error';
  service: string;
  message: string;
}

// 网络统计数据类型
export interface NetworkStats {
  inbound: number;
  outbound: number;
  activeConnections: number;
  totalTransferred: number;
  requestsPerMinute: number;
}

// 存储卷数据类型
export interface StorageVolume {
  name: string;
  usedPercentage: number;
  totalSpace: number;
  freeSpace: number;
}

// 数据库统计数据类型
export interface DatabaseStats {
  connections: number;
  activeQueries: number;
  cacheHitRate: number;
  avgQueryTime: number;
}

// 系统监控完整数据类型
export interface SystemMonitorData {
  systemResources: SystemResourceData;
  services: ServiceStatus[];
  logs: LogEntry[];
  networkStats: NetworkStats;
  storageVolumes: StorageVolume[];
  databaseStats: DatabaseStats;
}

/**
 * 系统监控服务
 */
export const systemMonitorService = {
  /**
   * 获取所有系统监控数据
   */
  getAllData: async (): Promise<SystemMonitorData> => {
    try {
      const response = await getSystemMonitorData();
      if (!response.success || !response.data) {
        throw new Error(response.message || '获取系统监控数据失败');
      }
      return response.data;
    } catch (error) {
      console.error('获取系统监控数据失败:', error);
      throw error;
    }
  },

  /**
   * 获取系统资源数据
   */
  getSystemResources: async (): Promise<SystemResourceData> => {
    try {
      const response = await getSystemResources();
      if (!response.success || !response.data) {
        throw new Error(response.message || '获取系统资源数据失败');
      }
      return response.data;
    } catch (error) {
      console.error('获取系统资源数据失败:', error);
      throw error;
    }
  },

  /**
   * 获取服务状态列表
   */
  getServiceStatuses: async (): Promise<ServiceStatus[]> => {
    try {
      const response = await getServiceStatuses();
      if (!response.success || !response.data) {
        throw new Error(response.message || '获取服务状态列表失败');
      }
      return response.data;
    } catch (error) {
      console.error('获取服务状态列表失败:', error);
      throw error;
    }
  },

  /**
   * 重启服务
   */
  restartService: async (serviceName: string): Promise<{ success: boolean; message: string }> => {
    try {
      const response = await apiRestartService(serviceName);
      if (!response.success) {
        throw new Error(response.message || `重启服务 ${serviceName} 失败`);
      }
      return { success: true, message: response.message || `服务 ${serviceName} 已成功重启` };
    } catch (error) {
      console.error(`重启服务 ${serviceName} 失败:`, error);
      throw error;
    }
  },

  /**
   * 获取系统日志
   */
  getLogs: async (limit?: number): Promise<LogEntry[]> => {
    try {
      const response = await getSystemLogs(limit);
      if (!response.success || !response.data) {
        throw new Error(response.message || '获取系统日志失败');
      }
      return response.data;
    } catch (error) {
      console.error('获取系统日志失败:', error);
      throw error;
    }
  },

  /**
   * 获取网络统计数据
   */
  getNetworkStats: async (): Promise<NetworkStats> => {
    try {
      const response = await getNetworkStats();
      if (!response.success || !response.data) {
        throw new Error(response.message || '获取网络统计数据失败');
      }
      return response.data;
    } catch (error) {
      console.error('获取网络统计数据失败:', error);
      throw error;
    }
  },

  /**
   * 获取存储卷信息
   */
  getStorageVolumes: async (): Promise<StorageVolume[]> => {
    try {
      const response = await getStorageVolumes();
      if (!response.success || !response.data) {
        throw new Error(response.message || '获取存储卷信息失败');
      }
      return response.data;
    } catch (error) {
      console.error('获取存储卷信息失败:', error);
      throw error;
    }
  },

  /**
   * 获取数据库统计信息
   */
  getDatabaseStats: async (): Promise<DatabaseStats> => {
    try {
      const response = await getDatabaseStats();
      if (!response.success || !response.data) {
        throw new Error(response.message || '获取数据库统计信息失败');
      }
      return response.data;
    } catch (error) {
      console.error('获取数据库统计信息失败:', error);
      throw error;
    }
  },

  /**
   * 添加日志条目
   */
  addLogEntry: async (level: string, service: string, message: string): Promise<{ success: boolean }> => {
    try {
      const response = await addSystemLogEntry(level, service, message);
      if (!response.success) {
        throw new Error(response.message || '添加日志条目失败');
      }
      return { success: true };
    } catch (error) {
      console.error('添加日志条目失败:', error);
      throw error;
    }
  }
}; 